﻿using ProgrammingEducationSystem.DbConfig;
using ProgrammingEducationSystem.Models;
using Microsoft.EntityFrameworkCore;

namespace ProgrammingEducationSystem.Repository.IRepository
{
    public class ClassRepository : BaseRepository<Class>,IClassRepository
    {
        public ClassRepository(AppDbCtx appDbCtx) : base(appDbCtx)
        {
        }

        public async Task DeleteByTeacherIdAsync(string id)
        {
            _appDbCtx.Classes.RemoveRange(
                 await _appDbCtx.Classes.Where(c =>c.TeacherId == id).ToListAsync()
                );
        }

        public async Task<List<Class>> FindByIds(List<Guid> ids)
        {
            return await _appDbCtx.Classes.Where(c => ids.Contains(c.Id)).ToListAsync();
        }

        public async Task<List<Class>?> GetByStudentIdAsync(string stuId)
        {
            var sac = await _appDbCtx.StuAndClasses.Where(sc => sc.StudentId == stuId).ToListAsync();
             return await _appDbCtx.Classes.Where(cs => sac.Any(sa => sa.ClassId == cs.Id)).ToListAsync();
        }

        public async Task<List<Class>> GetByTeacherId(string id)
        {
            return await _appDbCtx.Classes.Where(c => c.TeacherId == id).ToListAsync();
        }
    }
}
